package client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSON;

/**
 * http://hc.apache.org/httpclient-3.x/tutorial.html
 * 
 * @author pactera
 *
 */
public class HttpClientTutorial {

	private static String url = "http://www.apache.org/";

	public static void main(String[] args) {
		HttpClientTutorial hct = new HttpClientTutorial();
//		hct.invokeGet();
		hct.invokePost();
	}

	public void invokePost() {
		HttpClient client = new HttpClient();
		String uri = "http://localhost:8180/OpenApiRESTDemo/common/rest2/showUserJson";
		PostMethod method = new PostMethod(uri);

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", "111");

		String content = JSON.toJSONString(param);
		String contentType = "application/json";
		String charset = "UTF-8";
		RequestEntity requestEntity = null;
		try {
			requestEntity = new StringRequestEntity(content, contentType, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		method.setRequestEntity(requestEntity);

		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}

			byte[] responseBody = method.getResponseBody();

			System.out.println(new String(responseBody));
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}
	}

	public void invokeGet() {
		// Create an instance of HttpClient.
		HttpClient client = new HttpClient();

		// Create a method instance.
		GetMethod method = new GetMethod(url);

		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

		try {
			// Execute the method.
			int statusCode = client.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}

			// Read the response body.
			byte[] responseBody = method.getResponseBody();

			// Deal with the response.
			// Use caution: ensure correct character encoding and is not binary
			// data
			System.out.println(new String(responseBody));

		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
	}
}
